package com.yphone.ui.main;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.SparseArray;

import com.iiordanov.bVNC.App;
import com.yphone.base.utils.SharepreferenceUtils;
import com.yphone.net.IUIDataListener;
import com.yphone.net.bean.BaseResultWrapper;
import com.yphone.tools.SharepreferenConstants;
import com.yphone.tools.ToastUtil;
import com.yphone.ui.dialog.CommSmallLoadingDialog;
import com.yphone.ui.event.MyEvent;
import com.yphone.ui.login.manager.LoginManager;
import com.yphone.ui.login.model.LoginActivityModel;
import com.yphone.ui.login.respone.UserInfo;
import com.yphone.ui.ysj.bean.RemoteConnectBean;
import com.zzkj.yunphone.R;
import com.yphone.base.BasicFragment;
import com.yphone.tools.MyLog;
import com.yphone.ui.my.fragment.TabMyFragment;
import com.yphone.ui.ysj.fragment.TabYSJFragment;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

public class MainActivityPresenter implements MainActivityContract.IPrestenter {
    private final MainModel mMainModel;
    private boolean bNoReadCoupon = false;
    private FragmentManager fragmentManager;
    private String[] fragmentTags;
    /* access modifiers changed from: private */
    public MainActivityContract.IView iView;
    private String mPath;
    private SparseArray<Fragment> sIndexFragments;
    private ArrayList<TabData> tabDatas = new ArrayList<>();
    //    private TabExtendFragment tabExtendFragment;
//    private TabManageFragment tabManageFragment;
//    private TabMyFragment tabMyFragment;
    private TabYSJFragment tabYSJFragment;
    private TabMyFragment tabMyFragment;

    public class TabData {
        public BasicFragment fragment;
        public int tabItemResourceID;
        public String tabTags;

        public TabData() {
        }
    }

    public MainActivityPresenter(MainActivityContract.IView iView2) {
        this.iView = iView2;
        this.mMainModel = new MainModel();
    }

    public void start() {
        this.sIndexFragments = new SparseArray<>();
        this.fragmentManager = this.iView.getCurrentActivity().getSupportFragmentManager();
        if (this.tabYSJFragment == null) {
            this.tabYSJFragment = new TabYSJFragment();
        }
        this.fragmentManager.beginTransaction().add(R.id.main_fram_layout, this.tabYSJFragment, MainActivityContract.IPrestenter.YSJ_FRAGMENT_TAG).commitAllowingStateLoss();
        this.fragmentTags = new String[]{MainActivityContract.IPrestenter.YSJ_FRAGMENT_TAG, MainActivityContract.IPrestenter.EXTEND_FRAGMENT_TAG, MainActivityContract.IPrestenter.MANAGE_FRAGMENT_TAG, MainActivityContract.IPrestenter.MY_FRAGMENT_TAG};
        this.sIndexFragments.append(0, this.tabYSJFragment);
    }

    public BasicFragment getFragment(int position) {
        switch (position) {
            case 0:
                if (this.tabYSJFragment == null) {
                    this.tabYSJFragment = new TabYSJFragment();
                    this.sIndexFragments.append(position, this.tabYSJFragment);
                }
                return this.tabYSJFragment;
            case 1:
                if (this.tabMyFragment == null) {
                    this.tabMyFragment = new TabMyFragment();
                    this.sIndexFragments.append(position, this.tabMyFragment);
                }
                return this.tabMyFragment;


//                if (this.tabExtendFragment == null) {
//                    this.tabExtendFragment = new TabExtendFragment();
//                    this.sIndexFragments.append(position, this.tabExtendFragment);
//                }
//                return this.tabExtendFragment;
            case 2:
//                if (this.tabManageFragment == null) {
//                    this.tabManageFragment = new TabManageFragment();
//                    this.sIndexFragments.append(position, this.tabManageFragment);
//                }
//                return this.tabManageFragment;
            case 3:
//                if (this.tabMyFragment == null) {
//                    this.tabMyFragment = new TabMyFragment();
//                    this.sIndexFragments.append(position, this.tabMyFragment);
//                }
//                return this.tabMyFragment;
            default:
                return null;
        }
    }

    public void onTabSelected(int position) {
        BasicFragment fragment = getFragment(position);
        MyLog.i(BasicFragment.class.getSimpleName(), "classname:" + fragment.getClass().getName() + ",onTabSelected:" + position + ",isAdd:" + fragment.isAdded());
        FragmentTransaction fragmentTransaction = this.fragmentManager.beginTransaction();
        if (!fragment.isAdded()) {
            fragmentTransaction.add(R.id.main_fram_layout, fragment, this.fragmentTags[position]);
        } else {
            fragmentTransaction.show(fragment);
        }
        fragmentTransaction.commitAllowingStateLoss();
//        if (fragment instanceof TabMyFragment) {
//            getNoReadCouponInfo();
//        } else if (fragment instanceof TabExtendFragment) {
//            EventBus.getDefault().post(new RefreshExtendService());
//        }
    }

    public void onTabUnselected(int position) {
        FragmentTransaction fragmentTransaction = this.fragmentManager.beginTransaction();
        BasicFragment fragment = getFragment(position);
        MyLog.i(BasicFragment.class.getSimpleName(), "classname:" + fragment.getClass().getName() + ",onTabUnselected:" + position + ",isAdd:" + fragment.isAdded());
        fragmentTransaction.hide(fragment);
        fragmentTransaction.commitAllowingStateLoss();
    }

    public void destory() {
    }

    public void requestInit() {
//        CommSmallLoadingDialog.showDialog(iView.getCurrentContext(), "正在加载设备，请稍等...");
        mMainModel.requestInitDevice(new IUIDataListener() {
            @Override
            public void uiDataError(Exception exc) {
                ToastUtil.showToast("系统错误！");
                CommSmallLoadingDialog.dismissDialog();
            }

            @Override
            public void uiDataSuccess(Object obj) {
                CommSmallLoadingDialog.dismissDialog();
                BaseResultWrapper<List<RemoteConnectBean>> wrapper = (BaseResultWrapper) obj;
                if (wrapper.code != 1) {
                    ToastUtil.showToast(wrapper.msg);
                    return;
                }
                List<RemoteConnectBean> datas = wrapper.data;
                if (datas != null) {
                    UserInfo info = LoginManager.getInstance().getInfo();
                    if (info!=null) {
                        info.remoteConnectBeans = datas;
                        SharepreferenceUtils.saveClass(App.getContext(), SharepreferenConstants.SHARE_NAME_FILE, SharepreferenConstants.USER_INFO_NODE, info);
                        EventBus.getDefault().post(new MyEvent.LoginSuccessEvent(info, 0));
                    }
                }

            }
        });
    }

    @Override
    public void updateDeviceRemake(String id, String remake) {
        mMainModel.updateDeviceName(remake, id);
    }

}